
<?php
class Controller_Admin_Verify extends Controller_Template 
{
	//private $_verify;
	public function __construct ($request, $response)
	{
		parent::__construct($request, $response);
		$config = Kohana::config('verify');
		$this->template = 'smarty:tags/index';
		$this->_config = (array) $config;
		$module_names = array_keys($this->_config);
		$this->_display_name =$module_names[0];
	}

	
	
	/*
	 * 审核的默认页面，展示审核中默认第一项的数据记录
	 */
	public function action_list($page=1,$module_name=null)
	{	
		$module_name          = $module_name == '' ? $this->_display_name : trim($module_name);
		$modle_name           = $this->get_model_name($module_name);
		$this->_verify        = Model::factory($modle_name);
		$this->_page_size     = $this->_config[$module_name]['page_size'];
		$curr_display_name    = $this->_config[$module_name]['display_name'];
		
    	$page_config = array(
        	'total_items'       => $this->_verify->count_unverified(),
			'items_per_page'    => $this->_page_size,
			'current_page'      => array('source' => 'route', 'key' => 'param1'),
			//'current_page'      => array('source' => 'query_string', 'key' => 'page'),
			//'view'              => 'pagination/basic',
			//'auto_hide'         => TRUE,
			'first_page_in_url' => TRUE,
    	);

		$pagination = Pagination::factory($page_config);			
			
		$size   = $this->_page_size;
		$page   = intval($page) > 0 ? intval($page) : 1;
		$offset = $size * ($page - 1);		

		$data         = $this->_verify->list_unverified($offset,$size);
		$display_name = $this->display_name();

		
		$this->template->set('page_title', '审核');
		$this->template->set('data', $data);
		$this->template->set('curr_module_name', $module_name);
		$this->template->set('curr_display_name', $curr_display_name);
		$this->template->set('display_name', $display_name);
		$this->template->set('pagination', $pagination->render());
		$this->template->set_filename('smarty:admin/verify/index');

	}


	/*
	 * 设置一组记录通过审核
	 */
	public function action_set_verified($module_name)
	{
		$module_name          = $module_name == '' ? $this->_display_name : trim($module_name);
		$modle_name           = $this->get_model_name($module_name);
		$this->_verify        = Model::factory($modle_name);
		$this->_model_name    = $this->_config[$module_name]['model_name'];

		
		$ids = $_POST['ids'];
		$ids = rtrim($ids, '_');
		$ids = explode('_', $ids);

		$tip = "";
		$result = $this->_verify->set_verified($ids);
		if($result){
			$state = 1;
			$tip  .= "操作成功!";
		}
		else
		{
			$state = 0;
			$tip  .= "操作失败!";
		}
		$arr = array(
			"state" => $state,
			"tip"   => $tip,
		);
		
		$this->template->set('data',json_encode($arr));
		$this->template->set_filename('smarty:admin/verify/json_data');
		$this->response->body($this->template->render());
	}
	


	/*
	 * 设置一组记录未通过审核
	 */
	public function action_set_unverified ($module_name)
	{

		$module_name          = $module_name == '' ? $this->_display_name : trim($module_name);
		$modle_name           = $this->get_model_name($module_name);
		$this->_verify        = Model::factory($modle_name);
		$this->_model_name    = $this->_config[$module_name]['model_name'];

		
		$ids = $_POST['ids'];
		$reason  = $_POST['reason'];
		//$ids = '3_6_';
		//$reason = "this is refuse reason";
		$ids    = rtrim($ids, '_');
		$ids    = explode('_', $ids);

		$result = $this->_verify->set_unverified($ids,$reason);
		
		$tip = '';
		if($result){
			$state = 1;
			$tip  .= "操作成功!";
		}
		else
		{
			$state = 0;
			$tip  .= "操作失败!";
		}

		$data = array(
			"state" => $state,
			'tip'   => $tip,
			
		);
		
		$this->template->set('data',json_encode($data));
		$this->template->set_filename('smarty:admin/verify/json_data');
		$this->response->body($this->template->render());
	}
	

	/*
	 * 获取一个记录的详情记录
	 */
	public function action_get_detail_info($module_name,$id)
	{
		$module_name          = $module_name == '' ? $this->_display_name : trim($module_name);
		$modle_name           = $this->get_model_name($module_name);
		$this->_verify        = Model::factory($modle_name);

		$data = $this->_verify->get_detail_info($id);
		
		$this->template->set('data',json_encode($data));
		$this->template->set_filename('smarty:admin/verify/json_data');
		$this->response->body($this->template->render());
	}
	

	/*
	 * ajax调用时,通过一个display_name获取相应的model_name
	 */
	private function get_modelname($display_name)
	{
		$display_name = strval(trim($display_name));
		$config = $this->_config;

		$model_name = '';
		foreach ($config as $key => $val)
		{
			if($val['display_name'] == $display_name)
			{
				$model_name = $val['model_name'];
			}
		}
		return $model_name;
	}


	/*
	 * 获取一个审核的的所有数据记录
	 */
	private function list_unverified($start, $limit)
	{
		$result = $this->_verify->list_unverified($start,$limit);
		return $result;
	}


	/*
	 * 获取模型对象
	 */
	private function get_model_name($module_name)
	{
		return $this->_config[$module_name]['model_name'];
	}


	/*
	 * 获取审核模块的名称
	 */
	private function display_name()
	//public function action_display_name()
	{
    	$config = $this->_config;
		$display_name = array();
		$i=0;
		foreach ($config as $key => $val)
		{
			$display_name[$i]['display_name'] = $val['display_name'];
			$display_name[$i]['model_name'] = $key;
			$i++;
		}
		//print_r($display_name); exit;
		return $display_name;
	}

}
